package com.xunk.equipment.service.zhijia.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.xunk.equipment.domain.EquipmentDO;
import com.xunk.equipment.mapper.EquipmentMapper;
import com.xunk.equipment.service.BaseService;
import com.xunk.equipment.service.IFreshAirService;
import com.xunk.equipment.vo.response.FreshAirResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xunk.common.core.domain.EquipmentResponse;

import java.util.*;

/**
 * @ProjectName: HIM
 * @Package: com.xunk.equipment.service.zhijia.service
 * @ClassName: UhomeFreshAirService
 * @Author: luanzhiwei
 * @Description: 智家新风机service
 * @Date: 2021/6/16 9:49
 * @Version: 1.0
 */
@Service(value = "uplus-fresh-air")
public class UhomeFreshAirService implements IFreshAirService {

    @Autowired
    private ZhijiaService zhijiaService;
    @Autowired
    private EquipmentMapper equipmentMapper;
    @Autowired
    private BaseService baseService;


    /**
     * 获取设备当前状态值信息
     *
     * @param equipmentDO 当前设备信息，包含设备id，设备厂商
     * @return
     */
    @Override
    public EquipmentResponse getEquipmentInfo(EquipmentDO equipmentDO) {
        String deviceSn = equipmentDO.getDeviceSn();
        final Long homeId = equipmentMapper.selectHomeIdByDeviceSn(deviceSn);
        Map<String, Object> cacheMap = zhijiaService.getShadowByDeviceSn(homeId, equipmentDO.getDeptId(), deviceSn);
        FreshAirResponse response = new FreshAirResponse();
        response.setDeviceName(equipmentDO.getDeviceName());
        response.setDeviceType(equipmentDO.getDeviceType());
        response.setDeviceSn(deviceSn);
        //reported 包含全部设备属性
        final Map reported = (Map) cacheMap.get("reported");
        final Boolean onOffStatus = Convert.toBool(reported.get("onOffStatus"));
        response.setOn(onOffStatus);
        response.setFanLevel(Convert.toInt(reported.get("windSpeed")));
        response.setFanLevelList(fanLevelList);
        final List<Map<String, Object>> module = (List<Map<String, Object>>) cacheMap.get("module");
        final Map moduleInfo = module.get(0);
        final Map info = (Map) moduleInfo.get("moduleInfos");
        final Optional<Map<String, Object>> first = module.stream().filter(item -> ObjectUtil.equals(item.get("moduleType"), "basemodule")).findFirst();
        if (first.isPresent()) {
            final Map<String, Object> firstMap = first.get();
            final Map firstInfo = (Map) firstMap.get("moduleInfos");
            response.setModel(Convert.toStr(firstInfo.get("hardwareType")));
        }
        response.setFirmWare(Convert.toStr(info.get("softwareVers")));
        response.setSerialNumber(Convert.toStr(cacheMap.get("wifiType")));
        response.setSupplier(equipmentDO.getSupplier());
        response.setMac(Convert.toStr(cacheMap.get("deviceId")));
        response.setCandidateModel(baseService.getCandidateModel(equipmentDO.getDeviceBaseId()));
        return response;
    }

    /**
     * 控制设备
     *
     * @param equipmentDO 当前设备信息，包含设备id，设备厂商
     * @param type        控制类型，如温度、开关、模式
     * @param value       操作目标值  如 20°(20)、open(true)、冷风模式(3)
     * @return
     */
    @Override
    public Boolean control(EquipmentDO equipmentDO, String type, String value) {
        return zhijiaService.control(equipmentDO, type, value);
    }

    //风力预设值
    private List<Map<String, String>> fanLevelList = new ArrayList<Map<String, String>>(4) {{
        add(createMap("高", "1"));
        add(createMap("中", "2"));
        add(createMap("低", "3"));
    }};

    private Map<String, String> createMap(String key, String value) {
        Map<String, String> map = new HashMap<>();
        map.put(key, value);
        return map;
    }
}
